using System;
using System.Collections.Generic;
using System.Linq;
using ApiProject.Api.Data;
using ApiProject.Api.Entity;
using ApiProject.Api.PramaModel;
using ApiProject.Api.Repository;
using ApiProject.Api.Uitls;
using ApiProject.Api.Utils;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;

//商品
namespace ApiProject.Api.Controllers
{
    // [Authorize]
    [ApiController]
    [Route("[controller]")]
    public class PaymentController : ControllerBase
    {
        private IRepository<Payment> _paymentRepository;

        private readonly IWebHostEnvironment _hostingEnvironment;

        public PaymentController(
            IRepository<Payment> paymentRepository,
            IWebHostEnvironment hostingEnvironment
        )
        {
            _paymentRepository = paymentRepository;
            _hostingEnvironment = hostingEnvironment;
        }


        //添加支付信息
        // Post /payment
        [HttpPost]
        public dynamic AddPayment()
        {

            var files = HttpContext.Request.Form.Files;

            var paymentName = HttpContext.Request.Form["modeOfPayment"].ToString();

            string webRootPath = _hostingEnvironment.WebRootPath; // ...Api/wwwroot
            var showfilePath = PicHelper.getFilePath(files, "upload", webRootPath);

            var isPayment = _paymentRepository.Table.Where(x => x.ModeOfPayment == paymentName).FirstOrDefault();

            if (isPayment != null)
            {
                if (isPayment.IsDelete == true)
                {
                    var newPayment = new Payment
                    {
                        ModeOfPayment = paymentName,
                        PaymentPicturePath = showfilePath
                    };

                    _paymentRepository.Insert(newPayment);

                    return JsonHelper.Serialize(new
                    {
                        Code = 200,
                        Data = newPayment,
                        Msg = "添加支付方式成功"
                    });
                }
                else
                {
                    return JsonHelper.Serialize(new
                    {
                        Code = 1000,
                        Data = "",
                        Msg = "支付方式名称已存在，请重新输入"
                    });
                }

            }
            else
            {
                var newPayment = new Payment
                {
                    ModeOfPayment = paymentName,
                    PaymentPicturePath = showfilePath
                };

                _paymentRepository.Insert(newPayment);

                return JsonHelper.Serialize(new
                {
                    Code = 200,
                    Data = newPayment,
                    Msg = "添加支付方式成功"
                });
            }

        }


        // 查找所有支付方式
        // Get /payment
        [HttpGet]
        public dynamic GetPayment(){
            var allPayment = _paymentRepository.Table.Where(x => x.IsDelete == false).ToList();

            return JsonHelper.Serialize(new{
                Code = 200,
                Data = allPayment,
                Msg = "查找成功"
            });
        }



        // 是否启用该支付方式
        // Post /payment/id
        [HttpPost("{id}")]
        public dynamic EnablePayment(int id,NewPay model){

            var pay = _paymentRepository.GetById(id);
            var isEnable = model.IsEnable;

            if (pay != null)
            {
                if (isEnable == false)
                {
                    pay.IsEnable = false;
                    pay.UpdateTime = DateTime.Now;
                    _paymentRepository.Update(pay);
                    return JsonHelper.Serialize(new
                    {
                        Code = 200,
                        Data = pay,
                        Msg = string.Format("已禁用Id为{0}的支付方式", id)
                    });
                }
                else
                {
                    pay.IsEnable = true;
                    pay.UpdateTime = DateTime.Now;
                    _paymentRepository.Update(pay);
                    return JsonHelper.Serialize(new
                    {
                        Code = 200,
                        Data = pay,
                        Msg = string.Format("已启用Id为{0}的方式", id)
                    });
                }

            }
            else
            {
                return JsonHelper.Serialize(new { Code = 1000, Data = "", Msg = "禁用失败" });
            }
        }


        // 删除该支付方式
        // Delete /payment/id
        [HttpDelete("{id}")]
        public dynamic DeletePayment(int id){
            var pay = _paymentRepository.GetById(id);
            pay.IsDelete = true;
            pay.UpdateTime = DateTime.Now;
            _paymentRepository.Update(pay);
            return JsonHelper.Serialize(new{
                Code = 200,
                Data = pay,
                Msg = "删除该支付方式成功"
            });
        }


        //编辑支付方式
        //Put /payment/id
        [HttpPut("{id}")]
        public dynamic EditPayment(int id){

            var files = HttpContext.Request.Form.Files;

            var paymentName = HttpContext.Request.Form["modeOfPayment"].ToString();

            string webRootPath = _hostingEnvironment.WebRootPath; // ...Api/wwwroot
            var showfilePath = PicHelper.getFilePath(files, "upload", webRootPath);

            var isPayment = _paymentRepository.GetById(id);

            isPayment.ModeOfPayment = paymentName;
            isPayment.PaymentPicturePath = showfilePath;
            isPayment.UpdateTime = DateTime.Now;

            _paymentRepository.Update(isPayment);

            return JsonHelper.Serialize(new{
                Code = 200,
                Data = isPayment,
                Msg = "修改成功"
            });
        }




    }
}
